package com.woniuxy.escort.controller;

import com.woniuxy.commons.entity.Escort;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.util.PageMessage;
import com.woniuxy.commons.util.ResponseResult;
import com.woniuxy.commons.util.ResultStatus;
import com.woniuxy.escort.service.EscortService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;




@RestController
@Slf4j
@RequestMapping("/escort")
public class EscortController {


        @Resource
        private EscortService escortService;

        /**
         *
         * @param page
         * @param size
         * @return 分页查询所有陪护员附带查询陪护员的user信息
         */
        @GetMapping("/findAllEscort/{page}/{size}")
        public ResponseResult findAllEscort(@PathVariable("page") int page, @PathVariable("size") int size) {
            PageMessage<List<Escort>> escorts =  escortService.findAllEscort(page,size);
            ResponseResult responseResult = new ResponseResult();
            responseResult.setMsg("没有获取到数据");
            responseResult.setCode(500);
            if(!escorts.getData().isEmpty()){
                responseResult.setMsg("成功获取到数据");
                responseResult.setCode(200);
                responseResult.setData(escorts);
                responseResult.setStatus(ResultStatus.SUCCESS);
            }
            return responseResult;
        }
    //根据条件查询用户
    @PostMapping("/isExist")
    public ResponseResult<List<Escort>> isExist(@RequestBody Escort escort){
        log.info("前端传来的user"+escort.toString());
//        String i= user.getStatus();
//        if (i.equals("启用")){
//            user.setStatus(1);
//        }else {
//            user.setStatus(2);
//        }
//        log.info("修改过后的user"+user.toString());
        ResponseResult<List<Escort>> responseResult = new ResponseResult<>();
        List<Escort> escort1 = escortService.isExist(escort);
        log.info("修改过后的user"+escort1.toString());
        responseResult.setData(escort1);
        return responseResult;
    }

        /**
         *  新增一条陪护员信息
         */
        @PostMapping("/addEscort")
        public ResponseResult addEscort(@RequestBody Escort escort) {
            ResponseResult responseResult = new ResponseResult();
            boolean res = escortService.addEscort(escort);
            responseResult.setMsg("添加失败");
            responseResult.setCode(500);
            responseResult.setStatus(ResultStatus.FAIL);
            if(res){
                responseResult.setMsg("成功添加一条陪护员信息");
                responseResult.setCode(200);
                responseResult.setStatus(ResultStatus.SUCCESS);
            }
            return responseResult;
        }

        /**
         *  通过uid传入删除一条陪护员信息
         */
        @DeleteMapping("/delEscortByUid/{uid}")
        public ResponseResult delEscort(@PathVariable("uid") int uid) {
            ResponseResult responseResult = new ResponseResult();
            boolean res = escortService.delEscortByUid(uid);
            responseResult.setMsg("删除uid为"+uid+"的一条陪护员信息失败");
            responseResult.setCode(500);
            responseResult.setStatus(ResultStatus.FAIL);
            if(res){
                responseResult.setMsg("成功删除uid为"+uid+"的一条陪护员信息");
                responseResult.setCode(200);
                responseResult.setStatus(ResultStatus.SUCCESS);
            }
            return responseResult;
        }


        /**
         *  通过uid传入查询一条陪护员信息
         */
        @GetMapping("/findEscortByUid/{uid}")
        public ResponseResult findEscortByUid(@PathVariable("uid") int uid) {
            ResponseResult responseResult = new ResponseResult();
            Escort escort = escortService.findEscortByUid(uid);
            responseResult.setMsg("查询uid为"+uid+"的一条陪护员信息失败");
            responseResult.setCode(500);
            responseResult.setStatus(ResultStatus.FAIL);
            if(escort!=null){
                responseResult.setMsg("查询uid为"+uid+"的一条陪护员信息成功");
                responseResult.setCode(200);
                responseResult.setStatus(ResultStatus.SUCCESS);
                responseResult.setData(escort);
            }
            return responseResult;
        }

        /**
         *  通过传入car更新一条陪护员信息
         */
        @PutMapping("/updateEscort")
        public ResponseResult updateEscort(@RequestBody Escort escort) {
            ResponseResult responseResult = new ResponseResult();
            boolean res = escortService.updateEscort(escort);
            responseResult.setMsg("更新失败");
            responseResult.setCode(500);
            responseResult.setStatus(ResultStatus.FAIL);
            if(res){
                responseResult.setMsg("更新成功");
                responseResult.setCode(200);
                responseResult.setStatus(ResultStatus.SUCCESS);
                responseResult.setData(escort);
            }
            return responseResult;
        }


        //根据条件查询并分页
        @PostMapping("/findEscortByConditions/{page}/{size}")
        public ResponseResult findEscortByConditions(@PathVariable("page") int page, @PathVariable("size") int size,@RequestBody Escort escort) {
            ResponseResult responseResult = new ResponseResult();
            PageMessage<List<Escort>> escorts =  escortService.findEscortByConditions(escort,page,size);
            responseResult.setMsg("没有获取到数据");
            responseResult.setCode(500);
            if(!escorts.getData().isEmpty()){
                responseResult.setMsg("成功获取到数据");
                responseResult.setCode(200);
                responseResult.setData(escorts);
                responseResult.setStatus(ResultStatus.SUCCESS);
            }
            return responseResult;
        }


        //通过传入的uid值重置密码为默认密码，默认密码为123456
        @PutMapping("/resetPassword/{uid}")
        public ResponseResult resetPassword(@PathVariable("uid") int uid) {
            ResponseResult responseResult = new ResponseResult();
            boolean res = escortService.resetPassword(uid);
            responseResult.setMsg("重置失败");
            responseResult.setCode(500);
            responseResult.setStatus(ResultStatus.FAIL);
            if(res){
                responseResult.setMsg("重置成功");
                responseResult.setCode(200);
                responseResult.setStatus(ResultStatus.SUCCESS);
            }
            return responseResult;
        }


    }
